package com.cskaoyan.shopping.controller;

import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.commons.result.ResponseUtil;
import com.cskaoyan.mall.dto.ProductDetailDto;
import com.cskaoyan.shopping.dto.PanelContentDto;
import com.cskaoyan.shopping.dto.PanelDto;
import com.cskaoyan.shopping.dto.ProductCateDto;
import com.cskaoyan.shopping.service.IContentService;
import com.cskaoyan.shopping.service.IHomeService;
import com.cskaoyan.shopping.service.IProductCateService;
import com.cskaoyan.shopping.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author dinghongrui
 * @create 2022/09/20 20:35
*/
import com.cskaoyan.shopping.form.CheckAllItemRequest;
import com.cskaoyan.shopping.service.ICartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassName ShoppingController
 * @Description TODO
 * @Author zhangdongdong
 * @Date 2022/9/20 22:24
 * @Version 1.0
 */
@RestController
@RequestMapping("/shopping")
public class ShoppingController {

    @Autowired

    IHomeService iHomeService;

    @Autowired
    IContentService iContentService;

    @Autowired
    IProductCateService iProductCateService;

    @Autowired
    IProductService iProductService;

    @GetMapping("/homepage")
    public ResponseData getAllPanel() {
        List<PanelDto> panelDtoList = iHomeService.getPanel();

        if (panelDtoList == null) {
            return new ResponseUtil().setErrorMsg("系统正在维护");
        }

        return new ResponseUtil().setData(panelDtoList, "success");
    }

    @GetMapping("/navigation")
    public ResponseData getNavigation() {
        List<PanelContentDto> panelContentDtoList = iContentService.queryNavList();

        if (panelContentDtoList == null) {
            return new ResponseUtil().setErrorMsg("系统正在维护");
        }
        return new ResponseUtil().setData(panelContentDtoList, "success");
    }

    @GetMapping("/categories")
    public ResponseData getCategory(String sort) {
        List<ProductCateDto> allProductCate = iProductCateService.getAllProductCate(sort);

        if (allProductCate == null) {
            return new ResponseUtil().setErrorMsg("系统正在维护");
        }
        return new ResponseUtil().setData(allProductCate, "success");

    }

    @GetMapping("/product/{id}")
    public ResponseData getDetail(@PathVariable("id") long id) {
        ProductDetailDto productDetail = iProductService.getProductDetail(id);

        if (productDetail == null) {
            return new ResponseUtil().setErrorMsg("系统正在维护");
        }
        return new ResponseUtil().setData(productDetail, "success");

    }

    @Autowired
    private ICartService iCartService;


    /**
     * @description: 全选/全不选购物车
     * @param: checkAllItemRequest
     * @return: com.cskaoyan.mall.commons.result.ResponseData
     * @author zhangdongdong
     * @date: 2022/9/21 14:37
     */
    @PutMapping("/items")
    public ResponseData items(@RequestBody CheckAllItemRequest checkAllItemRequest) {
        iCartService.checkAllCartItem(checkAllItemRequest);
        return new ResponseUtil<>().setData("成功");
    }

    /**
     * @description: 删除购物车中的商品
     * @param: uid
pid
     * @return: com.cskaoyan.mall.commons.result.ResponseData
     * @author zhangdongdong
     * @date: 2022/9/21 14:38
     */
    @DeleteMapping("/carts/{uid}/{pid}")
    public ResponseData carts(@PathVariable Long uid, @PathVariable Long pid) {
        iCartService.deleteCartItem(uid, pid);
        return new ResponseUtil<>().setData("成功");
    }

    /**
     * @description: 删除购物车中选中的商品
     * @param: uid
     * @return: com.cskaoyan.mall.commons.result.ResponseData
     * @author zhangdongdong
     * @date: 2022/9/21 14:39
     */
    @DeleteMapping("/items/{uid}")
    public ResponseData carts(@PathVariable Long uid) {
        iCartService.deleteCheckedItem(uid);
        return new ResponseUtil<>().setData("成功");
    }
}
